package com.project.biz.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.biz.CmsOrderDetailBiz;
import com.project.biz.CmsOrderProductBakBiz;
import com.project.entity.CmsOrderDetail;
import com.project.entity.CmsOrderProductBak;
import com.project.entity.CmsProductSku;
import com.project.mapper.CmsOrderDetailMapper;
import com.project.pojo.dto.ProductOrderListItemDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 商品订单详情业务层接口实现类
 * @Author: zhuchi
 * @CreateDate: 2020/10/16 22:56
 * @Version: 1.0
 */
@Service
public class CmsOrderDetailBizImpl extends ServiceImpl<CmsOrderDetailMapper, CmsOrderDetail> implements CmsOrderDetailBiz {

    /**
     * 商品备份业务层接口类
     */
    @Autowired
    private CmsOrderProductBakBiz orderProductBakBiz;

    /**
     * 添加订单详情
     * @param orderDetails
     * @param orderId
     * @return
     */
    public int addOrderDetail(List<CmsOrderDetail> orderDetails, Long orderId){
        int result = 0;
        for (CmsOrderDetail orderDetail : orderDetails){
            orderDetail.setProductOrderId(orderId);
            int addOrderDetailState = this.baseMapper.insert(orderDetail);
            if (addOrderDetailState > 0){
                result++;
            }
        }

        return result;
    }

    /**
     * 获取订单商品信息,用于订单列表
     * @param orderId
     * @return
     */
    public List<ProductOrderListItemDTO> getProductOrderDetailList(Long orderId){
        List<ProductOrderListItemDTO> productOrderListItemDTOS = new ArrayList<>();

        //根据订单号查询订单商品信息
        List<CmsOrderDetail> list = this.getProductOrderDetails(orderId);
        if (null != list && list.size() > 0){
            ProductOrderListItemDTO productOrderListItemDTO;
            //备份商品信息
            CmsOrderProductBak orderProductBak;
            //备份商品标签信息
            List<String> productLabel;
            //备份商品SKU信息
            CmsProductSku productSku;
            for (CmsOrderDetail orderDetail : list){
                productOrderListItemDTO = new ProductOrderListItemDTO();
                BeanUtils.copyProperties(orderDetail, productOrderListItemDTO);
                //获取备份商品信息
                orderProductBak = this.orderProductBakBiz.getById(orderDetail.getProductBakId());
                productLabel = JSONObject.parseArray(orderProductBak.getProductLabel(), String.class);
                productSku = JSONObject.parseObject(orderProductBak.getProductSkus(), CmsProductSku.class);
                productOrderListItemDTO.setName(orderProductBak.getName());
                productOrderListItemDTO.setProductImg(orderProductBak.getProductImg());
                productOrderListItemDTO.setProductCode(orderProductBak.getProductCode());
                productOrderListItemDTO.setSupplier(orderProductBak.getSupplier());
                productOrderListItemDTO.setProductLabel(productLabel);
                productOrderListItemDTO.setSpecData(productSku.getSpecData());
                productOrderListItemDTO.setSkuImg(productSku.getSkuImg());
                productOrderListItemDTOS.add(productOrderListItemDTO);
            }
        }

        return productOrderListItemDTOS;
    }

    /**
     * 获取商品订单详情,仅订单详情表中数据
     * @param orderId
     * @return
     */
    public List<CmsOrderDetail> getProductOrderDetails(Long orderId){
        //根据订单号查询订单商品信息
        QueryWrapper<CmsOrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_order_id", orderId);
        List<CmsOrderDetail> list = this.baseMapper.selectList(queryWrapper);

        return list;
    }
}
